package com.dragon.www.smarthome.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.dragon.www.smarthome.R;
import com.dragon.www.smarthome.entity.Device;
import com.dragon.www.smarthome.entity.DeviceInfo;
import com.dragon.www.smarthome.net.NetHelper;
import com.dragon.www.smarthome.net.NetUtils;

/**
 * Created by liqp on 2016/3/16.
 */
public class CurtainAdapter extends  AppArrayAdapter<Device, CurtainAdapter.CurtainViewHolder>  {

    public CurtainAdapter(Context context){
        super(context, R.layout.curtain_view_item, CurtainViewHolder.class);
    }

    public View bindHolder(View convertView, ViewGroup parent, final int position) {
        final Device curtain = this.devices.get(position);
        CurtainViewHolder viewHolder;
        if(convertView == null){
            viewHolder = new CurtainViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.curtain_view_item, parent, false);
            convertView.setLayoutParams(new AbsListView.LayoutParams((int) (parent.getWidth() / 2) - 1, (int) (parent.getHeight() / 3)));// 动态设置item的高度
            viewHolder.tv_curtain_name = (TextView) convertView.findViewById(R.id.tv_curtain_desc);
            viewHolder.iv_curtain_up = (ImageView) convertView.findViewById(R.id.iv_curtain_up);
            viewHolder.iv_curtain_down = (ImageView) convertView.findViewById(R.id.iv_curtain_down);
            viewHolder.iv_curtain_stop = (ImageView) convertView.findViewById(R.id.iv_curtain_stop);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (CurtainViewHolder) convertView.getTag();
            convertView.setLayoutParams(new AbsListView.LayoutParams((int) (parent.getWidth() / 2) - 1, (int) (parent.getHeight() / 3)));// 动态设置item的高度
        }

        viewHolder.tv_curtain_name.setText(curtain.getDeviceName());

        viewHolder.iv_curtain_up.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    setAction(curtain,"up", 2);
                }
                return false;
            }
        });

        viewHolder.iv_curtain_down.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    setAction(curtain,"down", 1);
                }
                return false;
            }
        });

        viewHolder.iv_curtain_stop.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    setAction(curtain,"stop", 0);
                }
                return false;
            }
        });

        return convertView;
    }

    public void setAction (final Device curtain,String action,int value){
        NetHelper.sendAction(context, Integer.parseInt(curtain.getequipNo().split("-")[0]), 1, action, value, "curtain", new NetUtils.NetCallBack<DeviceInfo>() {

            @Override
            public void success(DeviceInfo rspData) {
                if (rspData.getError() == 0) {
                    Log.e("error", "操作成功");
                }
            }

            @Override
            public void failed(String msg) {

            }
        }, DeviceInfo.class);

    }

    public class CurtainViewHolder extends AppHolder {
        TextView tv_curtain_name;
        ImageView iv_curtain_up;
        ImageView iv_curtain_down;
        ImageView iv_curtain_stop;
    }
}
